 
import json
import requests
import pandas as pd


def strip_chars(subdomain_df):
    """Strip *.'s and www.'s"""
    subdomain_df = subdomain_df.str.lower()
    subdomain_df = subdomain_df.str.replace("www.", "", regex=True)
    subdomain_df = subdomain_df.str.replace("\*.", "", regex=True)
    return subdomain_df


class CrtSh:
    def __init__(self):
        self.results = set()
        self.raw = {}
        
    def scan(self, domain, timeout=30):
        try:
            crt_req = requests.get(f"https://crt.sh/?dNSName={domain}&output=json", timeout=timeout).json()
        except requests.exceptions.ConnectionError:
            print('Connect CrtSh error')
            crt_req = {}
            
        self.raw = crt_req

        if crt_req == {}:
            empty_df = pd.DataFrame(columns={domain: ""})
            return empty_df
        try:
            subdomain_df = pd.DataFrame.from_dict(crt_req)["name_value"]
            subdomain_df.replace(r"\n.*", "", regex=True, inplace=True)
            subdomain_df = strip_chars(subdomain_df)
            self.results = set(subdomain_df.drop_duplicates())
        except Exception as e:
            print(e)
            return
    
    

if __name__ == '__main__':
    print(CrtSh().scan('chinaunicom.cn'))